package com.neusoft.ysu.populationmanagement.service.impl;

import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.FloPop;
import com.neusoft.ysu.populationmanagement.mapper.FloPopMapper;
import com.neusoft.ysu.populationmanagement.service.IFloPopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zzh
 * @since 2025-06-13
 */
@Service
public class FloPopServiceImpl implements IFloPopService {
    @Autowired
    FloPopMapper floPopMapper;

    @Override
    public CommonResult selectAll() {
        return new CommonResult<>(floPopMapper.selectList(null));
    }

    @Override
    public CommonResult insertFloPop(FloPop floPop) {
        if (floPopMapper.selectByPopId(floPop.getPopId()).isEmpty()) {
            System.out.println("flo插入:" + floPop);
            floPop.setCreateTime(LocalDateTime.now());
            floPop.setIsDelete(TabelDic.IS_DELETE_NO);
            return new CommonResult<>(1, "流动人口信息插入成功", floPopMapper.insert(floPop));
        } else
            return new CommonResult<>(0, "插入失败,该条流动人口信息已存在", 0);
    }

    @Override
    public CommonResult updateFloPop(FloPop floPop) {
        List<FloPop> fpops = floPopMapper.selectByPopId(floPop.getPopId());
        if (fpops.size() == 0)
            return new CommonResult<>(0, "未检索到流动人口信息", 0);
        else if (fpops.size() > 1) {
            System.out.println("流动人口数据库错误-字段重复" + fpops);
            return new CommonResult<>(0, "流动人口数据库错误-字段重复", fpops);
        }

        int row = floPopMapper.updateByPopId(floPop);
        if (row < 1) {
            System.out.println("流动人口信息变更失败");
            return new CommonResult<>(0, "流动人口信息变更失败", row);
        }
        return new CommonResult<>(1, "流动人口信息变更成功", fpops.get(0));
    }

    /**
     * @param floPop FloPop
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author ecl
     * @description 流动人口注销（逻辑删除）
     * @date 2025-06-16
     */
    @Override
    public CommonResult deleteFlo(FloPop floPop) {
        if (floPopMapper.selectByPopId(floPop.getPopId()).size() == 0)
            return new CommonResult<>(0, "未检索到流动人口信息", 0);
        floPop.setIsDelete(1);
        int row = floPopMapper.updateByPopId(floPop);
        if (row < 1) {
            System.out.println("流动人口注销失败");
            return new CommonResult<>(0, "流动人口注销失败", row);
        }
        return new CommonResult<>(1, "流动人口注销成功", floPop);

    }
/**
 * @author ecl
 * @description 流动人口注销（逻辑删除）
 * @date 2025-06-16
 * @param pop_id int
 * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
 */
    @Override
    public CommonResult deleteFloPopByPopId(Long pop_id) {
        List<FloPop> floPops = floPopMapper.selectByPopId(pop_id);
        if (floPops.size() != 1) {
            return new CommonResult<>(0, "根据PopId删除流动人口失败,size!=1", floPops);
        }
        FloPop floPop = floPops.get(0);
        floPop.setIsDelete(TabelDic.IS_DELETE_YES);
        floPopMapper.updateByPopId(floPop);
        return new CommonResult<>(1, "根据PopId删除流动人口成功", floPop);
    }



}
